home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 13.2 KB | 501 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: BmpSel.cpp
- // Release Version: $ 1.0d1 $
- //
- // Author: Henri Lamiraux
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef BMPSEL_H
- #include "BmpSel.h"
- #endif
-
- #ifndef BMPPART_H
- #include "BmpPart.h"
- #endif
-
- #ifndef BMPFACET_H
- #include "BmpFacet.h"
- #endif
-
- // ----- Frameworks Includes -----
-
- #ifndef FWUTIL_H
- #include "FWUtil.h"
- #endif
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWFACET_H
- #include "FWFacet.h"
- #endif
-
- // ----- Graphic Includes -----
-
- #ifndef FWGC_H
- #include "FWGC.h"
- #endif
-
- #ifndef FWRECSHP_H
- #include "FWRecShp.h"
- #endif
-
- #ifndef FWPICCSHP_H
- #include "FWPicShp.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSUSTRM_H
- #include <FWSUStrm.h>
- #endif
-
- #ifndef FWMEMMGR_H
- #include <FWMemMgr.h>
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _SHAPE_
- #include <Shape.h>
- #endif
-
- #ifndef _DRAGDROP_
- #include <DragDrop.h>
- #endif
-
- #ifndef _XMPSESSN_
- #include <XMPSessM.h>
- #endif
-
- #ifndef _STDPROPS_
- #include <StdProps.h>
- #endif
-
- #ifndef _STDTYPES_
- #include <StdTypes.h>
- #endif
-
- #ifndef _TRNSFORM_
- #include <Trnsform.h>
- #endif
-
- #ifndef _TRANSLAT_
- #include <Translat.h>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__DRAG__)
- #include <Drag.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__TOOLUTILS__)
- #include <ToolUtils.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
- #include <math routines.h>
- #endif
-
- #pragma segment bitmappart
-
- //========================================================================================
- // •• class CBitmapSelection
- //========================================================================================
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::CBitmapSelection
- //---------------------------------------------------------------------------------------
-
- CBitmapSelection::CBitmapSelection() :
- fBitmapPart(NULL),
- fHasSelection(FALSE),
- fAntPattern(FW_kAntPat)
- {
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::InitBitmapSelection
- //---------------------------------------------------------------------------------------
-
- void CBitmapSelection::InitBitmapSelection(CBitmapPart* thePart)
- {
- InitSelection(thePart, FALSE, FALSE);
-
- fBitmapPart = thePart;
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::~CBitmapSelection
- //---------------------------------------------------------------------------------------
-
- CBitmapSelection::~CBitmapSelection()
- {
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::CloseSelection
- //---------------------------------------------------------------------------------------
-
- void CBitmapSelection::CloseSelection()
- {
- if (fHasSelection)
- {
- DrawAnts(fBitmapPart->GetActiveFrame()); // Erase ants
- fHasSelection = FALSE;
- }
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::DoClear
- //---------------------------------------------------------------------------------------
- // We don't support clear selection. Just return false because the document
- // hasn't changed.
-
- FW_Boolean CBitmapSelection::DoClear()
- {
- return FALSE;
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::SelectAll
- //---------------------------------------------------------------------------------------
-
- void CBitmapSelection::SelectAll()
- {
- // ----- Close the selection if there is one -----
- CloseSelection();
-
- // ----- Get the active frame and it's shape -----
- FW_CRect rect;
- FW_CFrame* activeFrame = fBitmapPart->GetActiveFrame();
- activeFrame->GetFrameShapeBounds(&rect);
- SetSelectRect(rect);
-
- // ----- Draw the first ants -----
- DrawAnts(activeFrame);
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::IsEmpty
- //---------------------------------------------------------------------------------------
-
- FW_Boolean CBitmapSelection::IsEmpty() const
- {
- return !fHasSelection;
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::ExternalizeSelection
- //---------------------------------------------------------------------------------------
-
- void CBitmapSelection::ExternalizeSelection(XMPStorageUnit* storageUnit, FW_CFrame* commandFrame, XMPCloneKind cloneKind)
- {
- FW_UNUSED(commandFrame);
- FW_UNUSED(cloneKind);
-
- // ----- Create the content property-----
- storageUnit->AddProperty(kXMPPropContents)->AddValue(fBitmapPart->GetContentPropertyValueType());
-
- FW_CBitmapShape selectionShape = fBitmapPart->GetBitmapShape()->GetBitmapParts(fSelectRect);
-
- FW_CStorageUnitSink sink(storageUnit);
- FW_CWritableStream stream(&sink);
- selectionShape->Flatten(stream);
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::InternalizeSelection
- //---------------------------------------------------------------------------------------
-
- FW_Boolean CBitmapSelection::InternalizeSelection(XMPStorageUnit* storageUnit, XMPCloneKind cloneKind)
- {
- FW_UNUSED(cloneKind);
-
- FW_Boolean internalized = FALSE;
-
- XMPTranslation *translate = GetPart()->GetSession()->GetTranslation();
- XMPType PICTType = translate->GetISOTypeFromPlatformType('PICT', kXMPPlatformDataType);
-
- // ----- If there is a property content -----
- if (storageUnit->Exists(kXMPPropContents, PICTType, 0))
- {
- storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, PICTType, (XMPValueIndex)0, kXMPPosUndefined);
- InternalizePICT(storageUnit);
- internalized = TRUE;
- }
- else if (storageUnit->Exists(kXMPPropContents, kBitmapPartKind, 0))
- {
- storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, kBitmapPartKind, (XMPValueIndex)0, kXMPPosUndefined);
- InternalizeBitmap(storageUnit);
- internalized = TRUE;
- }
-
- if (internalized)
- fBitmapPart->InvalidateAllFrames(kNoPresentation, NULL, NULL);
-
- return internalized;
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::InternalizePICT
- //---------------------------------------------------------------------------------------
-
- void CBitmapSelection::InternalizePICT(XMPStorageUnit* storageUnit)
- {
- unsigned long pictSize = storageUnit->GetSize();
-
- FW_PlatformHandle handle = FW_CMemoryManager::AllocateSystemHandle(pictSize);
- FW_CMemoryManager::LockSystemHandle(handle);
- storageUnit->GetValue(pictSize, (XMPValue)(*handle));
- FW_CMemoryManager::UnlockSystemHandle(handle);
-
- FW_CRect rect = (*((FW_PlatformPict)handle))->picFrame;
- rect.Place(0,0);
- FW_CBitmapShape selection((FW_PlatformPict)handle, rect);
- fBitmapPart->SetBitmap(selection);
-
- fBitmapPart->InvalidateAllFrames(kNoPresentation, NULL, NULL);
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::InternalizeBitmap
- //---------------------------------------------------------------------------------------
-
- void CBitmapSelection::InternalizeBitmap(XMPStorageUnit* storageUnit)
- {
- FW_CBitmapShape bitmapShape = fBitmapPart->GetBitmapShape();
- FW_CStorageUnitSink sink(storageUnit);
- FW_CReadableStream stream(&sink);
- bitmapShape->Unflatten(stream);
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::GetSelectionShape
- //---------------------------------------------------------------------------------------
- // Return the shape of the selection. This will be added to the clipboard or
- // drag and drop storage unit.
-
- XMPShape* CBitmapSelection::GetSelectionShape()
- {
- FW_CRect rect(fSelectRect);
- rect.Offset(-rect.left, -rect.top);
- return ::NewXMPShape(rect);
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::MoveAnts
- //---------------------------------------------------------------------------------------
-
- void CBitmapSelection::MoveAnts()
- {
- FW_CFrame *frame = fBitmapPart->GetActiveFrame();
- if (frame != NULL)
- {
- FW_CGraphicContext gc(*frame->GetActiveFacet());
-
- DoDrawAnts(&gc);
-
- fAntPattern.ShiftRight();
-
- DoDrawAnts(&gc);
- }
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::DrawAnts
- //---------------------------------------------------------------------------------------
- // Draw ants in all facets of the given frame.
-
- void CBitmapSelection::DrawAnts(FW_CFrame* frame)
- {
- if (frame != NULL)
- {
- FW_CFrameFacetIterator ite(frame);
- while (!ite.IsDone())
- {
- FW_CFacet* facet = ite.CurrentItem();
-
- {
- FW_CGraphicContext gc(*facet);
- DoDrawAnts(&gc);
- }
-
- ite.Next();
- }
- }
- }
-
- //---------------------------------------------------------------------------------------
- // • CBitmapSelection::DoDrawAnts
- //---------------------------------------------------------------------------------------
-
- void CBitmapSelection::DoDrawAnts(FW_CGraphicContext* gc)
- {
- CBitmapFacet* facet = (CBitmapFacet*)FW_CFacet::XMPtoFWFacet(gc->GetXMPFacet());
- FW_CPoint ratio = facet->GetZoomRatio();
-
- FW_CRect rect;
- GetSelectRectWithZoomFactor(facet, rect);
-
- FW_CRectShape rectShape(rect);
- rectShape->SetPattern(fAntPattern);
- rectShape->SetTransferMode(FW_kXOr);
- rectShape->SetShapeFill(FW_kFramed);
- rectShape->Draw(gc);
- }
-
- //-------------------------------------------------------------------------
- // CBitmapSelection::Track
- //-------------------------------------------------------------------------
- // Tracker for the selection. This should be replace later by a tracker object.
-
- FW_Boolean CBitmapSelection::Track(CBitmapFacet* facet, const FW_CPoint& anchorPoint, XMPEventData event, FW_CRect& rect)
- {
- if (!::WaitMouseMoved(event->where))
- return FALSE;
-
- FW_CRect maxRect;
- facet->GetFrame()->GetFrameShapeBounds(&maxRect);
-
- FW_CGraphicContext gc(*facet);
-
- FW_CRectShape rectShape(rect);
- rectShape->SetPattern(FW_kAntPat);
- rectShape->SetTransferMode(FW_kXOr);
- rectShape->SetShapeFill(FW_kFramed);
-
- FW_CPoint currentLoc;
- FW_CPoint prevLoc = anchorPoint;
-
- FW_Boolean stillDown = TRUE;
- FW_Boolean erase = FALSE;
-
- while (stillDown)
- {
- FW_SPlatformPoint qdLoc;
- ::GetMouse(&qdLoc);
- currentLoc = qdLoc;
-
- // ----- Adjust for the size of the cursor
- if (anchorPoint.x < currentLoc.x)
- currentLoc.x++;
- if (anchorPoint.y < currentLoc.y)
- currentLoc.y++;
-
- // ----- Test if moved
- if (prevLoc != currentLoc)
- {
- if (erase)
- rectShape->Draw(&gc);
-
- // ----- Calcul rectangle -----
- if (anchorPoint.x < currentLoc.x)
- {
- rect.left = anchorPoint.x;
- rect.right = currentLoc.x;
- }
- else
- {
- rect.left = currentLoc.x;
- rect.right = anchorPoint.x + ff(1);
- }
-
- if (anchorPoint.y < currentLoc.y)
- {
- rect.top = anchorPoint.y;
- rect.bottom = currentLoc.y;
- }
- else
- {
- rect.top = currentLoc.y;
- rect.bottom = anchorPoint.y + ff(1);
- }
-
- rect &= maxRect;
-
- rectShape->SetRectangle(rect);
-
- // ----- Draw it -----
- rectShape->Draw(&gc);
-
- prevLoc = currentLoc;
- erase = TRUE;
- }
-
- stillDown = ::StillDown();
- }
-
- if (erase)
- rectShape->Draw(&gc);
-
- // ----- Apply the zoom ratio ----
- FW_CPoint ratio = facet->GetZoomRatio();
- rect.left = ::FixDiv(rect.left, ratio.x);
- rect.right = ::FixDiv(rect.right, ratio.x);
- rect.top = ::FixDiv(rect.top, ratio.y);
- rect.bottom = ::FixDiv(rect.bottom, ratio.y);
-
- return (currentLoc != anchorPoint);
- }
-
- //-------------------------------------------------------------------------
- // CBitmapSelection::InSelection
- //-------------------------------------------------------------------------
- // Test if a point is in the selection.
-
- FW_Boolean CBitmapSelection::InSelection(const FW_CPoint& where) const
- {
- if (IsEmpty())
- return FALSE;
-
- return fSelectRect.Contains(where);
-
- }
-
- //-------------------------------------------------------------------------
- // CBitmapSelection::CalcDragRgn
- //-------------------------------------------------------------------------
- // Calculate the region for the Drag and Drop Manager.
-
- XMPRgnHandle CBitmapSelection::CalcDragRgn(FW_CFacet* facet)
- {
- FW_UNUSED(facet);
-
- FW_CRect selectRect;
- GetSelectRectWithZoomFactor((CBitmapFacet*)facet, selectRect);
-
- FW_SPlatformRect rect = selectRect;
-
- XMPRgnHandle dragRgn = ::NewRgn();
- XMPRgnHandle tempRgn = ::NewRgn();
-
- ::RectRgn(dragRgn, &rect);
- ::CopyRgn(dragRgn, tempRgn);
- ::InsetRgn(tempRgn, 1, 1);
- ::DiffRgn(dragRgn, tempRgn, dragRgn);
-
- ::DisposeRgn(tempRgn);
-
- return dragRgn;
- }
-
- //-------------------------------------------------------------------------
- // CBitmapSelection::GetSelectRectWithZoomFactor
- //-------------------------------------------------------------------------
-
- void CBitmapSelection::GetSelectRectWithZoomFactor(CBitmapFacet* facet, FW_CRect& selectRect)
- {
- FW_CPoint ratio = facet->GetZoomRatio();
- selectRect.Set(::FixMul(fSelectRect.left, ratio.x),
- ::FixMul(fSelectRect.top, ratio.y),
- ::FixMul(fSelectRect.right, ratio.x),
- ::FixMul(fSelectRect.bottom, ratio.y));
- }